package hedron.dialog;

import hedron.Application;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class StringDialog extends Dialog {
	private Text string;
	private String text;
	private String defaultValue;

	public StringDialog(Shell parentShell, String text, String defaultValue) {
		super(parentShell);
		this.text = text;
		this.defaultValue = defaultValue;
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite grid = new Composite(parent, 0);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		grid.setLayout(layout);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));

		{// ----------------------------------------------------------
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 3;
			label.setLayoutData(gd);
			label.setText(text);
		}
		{
			{
				string = new Text(grid, SWT.BORDER);
				GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
				gd.widthHint = 350;
				gd.horizontalSpan = 3;
				string.setLayoutData(gd);
				string.setText(defaultValue);
				string.setSelection(0, defaultValue.length());
			}
			{
				Label label = new Label(grid, 0);
				GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
				label.setLayoutData(gd);
			}
			{// cancel
				Button button = new Button(grid, SWT.PUSH);
				button.setText("Cancel");
				GridData gd = new GridData(GridData.END, GridData.CENTER, false, false);
				gd.widthHint = 80;
				button.setLayoutData(gd);
				button.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						setReturnCode(CANCEL);
						close();
					}
				});
			}
			{
				Button button = new Button(grid, SWT.PUSH);
				button.setText("OK");
				GridData gd = new GridData(GridData.END, GridData.CENTER, false, false);
				gd.widthHint = 80;
				button.setLayoutData(gd);
				button.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {
						setReturnCode(OK);
						defaultValue = string.getText();
						close();
					}
				});

				// set button as default
				Shell shell = grid.getShell();
				if (shell != null) {
					shell.setDefaultButton(button);
				}
			}
		}
		grid.pack();
		return grid;
	}

	// We allow the user to resize this dialog
	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	public int open() {
		Application.placeDialogInCenter(getParentShell(), getShell());
		return super.open();
	}

	public String getString() {
		return defaultValue;
	}
}
